#pragma once
#ifndef MYPHONG_H
#define MYPHONG_H
#include <cmath>
#include "MyPointLight.h"

class MyPhong{

public:
	MyColor difuse;
	MyColor specular;
	MyColor color;
	std::vector<MyPointLight> lights;

	MyPhong(){
			
	}

	void setDifuse(MyColor lightColor, float r,float g, float b, float difuseCoef, float cosLN);
	void setSpecular(MyColor lightColor, float specularCoef, float cos, float shine);
	MyColor getColor(MyMaterial m,MyVector3 N, MyVector3 E, MyVector3 point, MyVector3 lightPosition, MyColor lightColor);
	void clearColor();
};

void MyPhong::clearColor(){

	this->color.r=0.0f;
	this->color.g=0.0f;
	this->color.b=0.0f;
}

void MyPhong::setDifuse(MyColor lightColor, float r,float g, float b, float difuseCoef, float cosLN){

	
	this->difuse.r = lightColor.r*r*difuseCoef*cosLN;
	this->difuse.g = lightColor.g*g*difuseCoef*cosLN;
	this->difuse.b = lightColor.b*b*difuseCoef*cosLN;
	
}

void MyPhong::setSpecular(MyColor lightColor, float specularCoef, float cos, float shine){

	
	this->specular.r = lightColor.r*specularCoef*pow(cos,shine);
	this->specular.g = lightColor.g*specularCoef*pow(cos,shine);
	this->specular.b = lightColor.b*specularCoef*pow(cos,shine);
	
}

MyColor MyPhong::getColor(MyMaterial m, MyVector3 N, MyVector3 E, MyVector3 point, MyVector3 lightPosition, MyColor lightColor){

	
	
	float cosLN, cosB;
	MyVector3 R,L;
	this->clearColor();

	//for(unsigned int i = 0; i < this->lights.size(); i++){
	
	L = MyVector3::vectorFromTo(point,lightPosition);
		L.normalize();
		cosLN = L.scalarMult(N);
		
		R.x = (float)(2.0*cosLN*N.x)-L.x;
		R.y = (float)(2.0*cosLN*N.y)-L.y;
		R.z = (float)(2.0*cosLN*N.z)-L.z;
	
		E.turnVector();
		E.normalize();
		R.normalize();
		cosB = E.scalarMult(R);

		if(cosB < 0.0f) cosB = 0.0f;
		//if(cosLN < 0.0f) cosLN = 0.0f;
		//printf("%f ",m.shine);
		this->setDifuse(lightColor,m.r,m.g,m.b,m.kd,cosLN);
		this->setSpecular(lightColor,m.ks,cosB,m.shine);
	
		this->color.r += this->difuse.r + this->specular.r;
		this->color.g += this->difuse.g + this->specular.g;
		this->color.b += this->difuse.b + this->specular.b;
		/*
		this->color.r += this->difuse.r;
		this->color.g += this->difuse.g;
		this->color.b += this->difuse.b;
		*/
		
	//}	

	return this->color;
	

} 

#endif

